home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 3 of 3 / CHAPTER6 / PUTDATA.C < prev    next >
C/C++ Source or Header  |  1996-04-27  |  15KB  |  384 lines

  1.  
  2. #include <windows.h>  
  3. #include <stdio.h>
  4. #include "PutData.h"
  5. #include "sqlext.h"  
  6.  
  7.  
  8. #if defined (WIN32)
  9.     #define IS_WIN32 TRUE
  10. #else
  11.     #define IS_WIN32 FALSE
  12. #endif
  13.  
  14. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  15. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  16. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  17.  
  18. HINSTANCE hInst;   // current instance
  19.  
  20. LPCTSTR lpszAppName = "MyApp";
  21. LPCTSTR lpszTitle   = "SQLPutData()"; 
  22.  
  23.  
  24. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  25.  
  26.  
  27. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  28.                       LPTSTR lpCmdLine, int nCmdShow)
  29. {
  30.    MSG      msg;
  31.    HWND     hWnd; 
  32.    WNDCLASS wc;
  33.  
  34.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  35.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  36.    wc.cbClsExtra    = 0;                      
  37.    wc.cbWndExtra    = 0;                      
  38.    wc.hInstance     = hInstance;              
  39.    wc.hIcon         = LoadIcon (hInstance, lpszAppName); 
  40.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  41.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  42.    wc.lpszMenuName  = lpszAppName;              
  43.    wc.lpszClassName = lpszAppName;              
  44.  
  45.    if ( IS_WIN95 )
  46.    {
  47.       if ( !RegisterWin95( &wc ) )
  48.          return( FALSE );
  49.    }
  50.    else if ( !RegisterClass( &wc ) )
  51.       return( FALSE );
  52.  
  53.    hInst = hInstance; 
  54.  
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107.  
  108. // buffer lengths
  109. // ..............
  110. #define NAME_LEN 20
  111.  
  112. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  113. {
  114. static HENV  hEnv  = NULL;
  115. static HDBC  hDBC  = NULL;
  116. static HWND  hList = NULL;
  117. static PTR   pParamData;
  118.  
  119. static int   rgbDeptId;
  120. static UCHAR rgbDeptName  [NAME_LEN+1];
  121. static int   rgbDeptHeadId;
  122.  
  123. static UCHAR szSqlStr[] = 
  124.    "Insert into department( dept_id, dept_name, dept_head_id ) "
  125.    "Values ( ?, ?, ? )";
  126. static UCHAR szLBStr[128];
  127.  
  128. // data being sent to the employee table.
  129. // ......................................
  130.  
  131. static int   nDeptId    [2]             = { 600,          700          };
  132. static UCHAR sDeptName  [2][NAME_LEN+1] = { "Accounting", "Accounting" };
  133. static int   nDeptHeadId[2]             = { 501,          703          };
  134.  
  135. static SDWORD cbIdLen;
  136. static SDWORD cbNameLen;
  137. static SDWORD cbHeadIdLen;
  138.  
  139.    switch( uMsg )
  140.    {
  141.       case WM_CREATE :
  142.               {
  143.                  RETCODE retCode;
  144.  
  145.                  // Allocate Environment and Connection.
  146.                  //.....................................
  147.                  SQLAllocEnv( &hEnv );
  148.                  SQLAllocConnect( hEnv, &hDBC );
  149.  
  150.                  // Connect to the data source
  151.                  //...........................
  152.                  retCode = SQLConnect( hDBC, 
  153.                              "Test Data Source", SQL_NTS,
  154.                              "DBA",              SQL_NTS,  
  155.                              "SQL",              SQL_NTS );
  156.  
  157.                  if ( retCode != SQL_SUCCESS )
  158.                  {
  159.                     SQLFreeConnect( hDBC );
  160.                     SQLFreeEnv( hEnv );
  161.                     return( -1 );
  162.                  }
  163.  
  164.                  if ( retCode == SQL_SUCCESS )
  165.                  {
  166.                     hList = CreateWindow( "LISTBOX", "",    
  167.                                           LBS_NOTIFY | WS_VSCROLL | 
  168.                                           WS_BORDER  | WS_CHILD | 
  169.                                           WS_VISIBLE | LBS_NOINTEGRALHEIGHT, 
  170.                                           0, 0, 
  171.                                           0, 0,  
  172.                                           hWnd,              
  173.                                           (HMENU)101,              
  174.                                           hInst,         
  175.                                           NULL               
  176.                                         );
  177.                  }
  178.               }
  179.               break;
  180.  
  181.       case WM_SIZE :
  182.               MoveWindow( hList, 0, 0, LOWORD( lParam ), 
  183.                                        HIWORD( lParam ), TRUE );
  184.               break; 
  185.               
  186.       case WM_COMMAND :
  187.               switch( LOWORD( wParam ) )
  188.               {
  189.                  case IDM_TEST :
  190.                         {
  191.                            HSTMT   hStmt;
  192.                            RETCODE retCodeExecute, retCodeParamData;
  193.                            int     nRowIdx = 0;
  194.  
  195.                            // Set connection to Manual commit mode
  196.                            // ..........................................
  197.                            SQLSetConnectOption( hDBC, SQL_AUTOCOMMIT, SQL_AUTOCOMMIT_OFF );
  198.  
  199.                            // Allocate a statement handle for the query.
  200.                            // ..........................................
  201.                            SQLAllocStmt( hDBC, &hStmt );
  202.  
  203.                            // Call SQLPrepare() to compile the query.
  204.                            // .......................................
  205.                            retCodeExecute = SQLPrepare( hStmt, szSqlStr, SQL_NTS );
  206.                            
  207.                            if( retCodeExecute != SQL_SUCCESS && 
  208.                                retCodeExecute != SQL_SUCCESS_WITH_INFO )
  209.                            {
  210.                               SQLFreeStmt( hStmt, SQL_DROP );
  211.                               break;
  212.                            }
  213.                            
  214.                            // Call SQLBindParameter() to 
  215.                            // bind buffers for the columns.
  216.                            // .............................
  217.                            retCodeExecute = SQLBindParameter( hStmt, 1, SQL_PARAM_INPUT, 
  218.                                              SQL_C_DEFAULT, SQL_INTEGER,
  219.                                              0, 0, 
  220.                                              &rgbDeptId, 0, 
  221.                                              &cbIdLen );
  222.                            
  223.                            retCodeExecute = SQLBindParameter( hStmt, 2, SQL_PARAM_INPUT, 
  224.                                              SQL_C_CHAR, SQL_CHAR, 
  225.                                              NAME_LEN, 0, 
  226.                                              rgbDeptName, 0, 
  227.                                              &cbNameLen );
  228.  
  229.                            retCodeExecute = SQLBindParameter( hStmt, 3, SQL_PARAM_INPUT, 
  230.                                              SQL_C_DEFAULT, SQL_INTEGER, 
  231.                                              0, 0, 
  232.                                              &rgbDeptHeadId, 0, 
  233.                                              &cbHeadIdLen );
  234.                            
  235.                            // Initialize the length values so we can 
  236.                            // pass length data at execution time.
  237.                            // ......................................
  238.                            cbIdLen     = SQL_LEN_DATA_AT_EXEC(0);
  239.                            cbNameLen   = SQL_LEN_DATA_AT_EXEC(0);
  240.                            cbHeadIdLen = SQL_LEN_DATA_AT_EXEC(0);
  241.  
  242.                            // Call SQLExecute() to execute the query.
  243.                            // .......................................
  244.                            retCodeExecute = SQLExecute( hStmt );
  245.  
  246.                            while( nRowIdx < 2 && retCodeExecute == SQL_NEED_DATA )
  247.                            {
  248.                               // Get a pointer to the parameter data
  249.                               // sent to SQLBindParameter().
  250.                               // ...................................
  251.                               retCodeParamData = SQLParamData( hStmt, &pParamData );
  252.  
  253.                               if( retCodeParamData == SQL_NEED_DATA )
  254.                               {
  255.                                  // Call SQLPutData() to fill parameter 
  256.                                  // data for the columns.
  257.                                  // ...................................
  258.                                  if( pParamData == &rgbDeptId )
  259.                                  {
  260.                                     SQLPutData( hStmt, &nDeptId[nRowIdx], SQL_NTS );
  261.  
  262.                                     sprintf( szLBStr, "DeptId %d added", 
  263.                                                       nDeptId[nRowIdx] );
  264.  
  265.                                     SendMessage( hList, LB_ADDSTRING, 0,
  266.                                        (LPARAM)szLBStr );
  267.                                  }
  268.                                  else if ( pParamData == rgbDeptName )
  269.                                  {
  270.                                     SQLPutData( hStmt, sDeptName[nRowIdx], SQL_NTS );
  271.  
  272.                                     sprintf( szLBStr, "DeptName %s added", 
  273.                                                       sDeptName[nRowIdx] );
  274.  
  275.                                     SendMessage( hList, LB_ADDSTRING, 0,
  276.                                        (LPARAM)szLBStr );
  277.                                  }
  278.                                  else if( pParamData == &rgbDeptHeadId )
  279.                                  {
  280.                                     SQLPutData( hStmt, &nDeptHeadId[nRowIdx], SQL_NTS );
  281.  
  282.                                     sprintf( szLBStr, "DeptHead %d added", 
  283.                                                       nDeptHeadId[nRowIdx] );
  284.  
  285.                                     SendMessage( hList, LB_ADDSTRING, 0,
  286.                                        (LPARAM)szLBStr );
  287.                                  }
  288.                               }
  289.                               else
  290.                               {
  291.                                  // Increment index into data.
  292.                                  // ..........................
  293.                                  nRowIdx++;
  294.  
  295.                                  if( nRowIdx < 2 )
  296.                                  {
  297.                                     retCodeExecute = SQLExecute( hStmt );
  298.                                  }
  299.                               }
  300.                            }
  301.                            
  302.                            // Successful transactions occurred, the last value
  303.                            // in retCodeExecute happens to be SQL_NEED_DATA.
  304.                            // This is a result of the logic, note that the 
  305.                            // 'SQL_NEED_DATA' request has been processed by now.
  306.                            // ..................................................
  307.                            if( nRowIdx == 2 && retCodeExecute == SQL_NEED_DATA )
  308.                            {
  309.                               // Commit the transaction.
  310.                               // .......................
  311.                               SQLTransact( SQL_NULL_HENV, hDBC, SQL_COMMIT );
  312.  
  313.                               SendMessage( hList, LB_ADDSTRING, 
  314.                                            0, (LPARAM)"Inserts successful." );
  315.                            }
  316.                            else
  317.                            {
  318.                               // Rollback the transaction.
  319.                               // .........................
  320.                               SQLTransact( SQL_NULL_HENV, hDBC, SQL_ROLLBACK);
  321.  
  322.                               SendMessage( hList, LB_ADDSTRING, 
  323.                                            0, (LPARAM)"Inserts failed." );
  324.                            }
  325.                            
  326.                            // Free the statement handle.
  327.                            // ..........................
  328.                            SQLFreeStmt( hStmt, SQL_DROP );
  329.                         }
  330.                         break;
  331.  
  332.                  case IDM_ABOUT :
  333.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  334.                         break;
  335.  
  336.                  case IDM_EXIT :
  337.                         DestroyWindow( hWnd );
  338.                         break;
  339.               }
  340.               break;
  341.       
  342.       case WM_DESTROY :
  343.               PostQuitMessage(0);
  344.  
  345.               // Disconnect Environment.
  346.               //........................
  347.               SQLDisconnect( hDBC );
  348.  
  349.               // Free Connection and Environment.
  350.               //.................................
  351.               SQLFreeConnect( hDBC );
  352.               SQLFreeEnv( hEnv );
  353.               break;
  354.  
  355.       default :
  356.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  357.    }
  358.  
  359.    return( 0L );
  360. }
  361.  
  362. LRESULT CALLBACK About( HWND hDlg,           
  363.                         UINT message,        
  364.                         WPARAM wParam,       
  365.                         LPARAM lParam)
  366. {
  367.    switch (message) 
  368.    {
  369.        case WM_INITDIALOG: 
  370.                return (TRUE);
  371.  
  372.        case WM_COMMAND:                              
  373.                if (   LOWORD(wParam) == IDOK         
  374.                    || LOWORD(wParam) == IDCANCEL)    
  375.                {
  376.                        EndDialog(hDlg, TRUE);        
  377.                        return (TRUE);
  378.                }
  379.                break;
  380.    }
  381.  
  382.    return (FALSE); 
  383. }
  384.